<?php


	/**
    * @framework : My Framework
    *
    * @author: fatangel26@gmail.com
    *
    */

	class product_controller extends my_controller
	{
		public function __construct()
        {
            parent::__construct();

            $this->_model->product = new model_product();
            $this->_model->menu    = new model_menu();
            $this->_helper         = new helper_text();
        }


        public function index()
        {
            $slug = $_GET['slug'];

            /*CHECK EXISTS SLUG*/
            if($slug == '' || is_numeric($slug) || !isset($slug))
            {
                $this->_data['head_name'] = '404';
                $this->_data['title']     = 'Jewel Shop';
                $this->_data['content']   = $this->view('errors/errors', $data = '', TRUE);
            }
            else
            {
                /*CHECK MENU HAS CHILDS*/
                $childs = $this->_model->menu->getAllChilds($slug);
                /*echo $childs;*/

                if($childs)
                {
                    $childs = explode(",", $childs);

                    /*NEED CHECK*/
                    foreach($childs as $key => $value):
                        if($value == "")
                            unset($childs[$key]);
                    endforeach;

                    $childs = array_unique($childs);

                    $data['products'] = $this->_model->product->getAllProducts($childs);
                    /*echo "<pre>";print_r($data['products']);*/
                }
                else
                {
                    $data['products'] = $this->_model->product->getProductBySlug(array('menu_slug' => $slug));
                }

                /*NEED CHECK*/
                if(!$data['products'])
                {
                    $this->_data['head_name'] = '404';
                    $this->_data['title']     = 'Jewel Shop';
                    $this->_data['content']   = $this->view('errors/errors', $data = '', TRUE);
                }
                else
                {
                    $head_name 					  = $this->_model->menu->getMenuName(array('slug' => $slug));

                    if($head_name):
                        $this->_data['head_name'] = $head_name['menu_name'];
                    else:
                        $this->_data['head_name'] = 'Sản phẩm';
                    endif;

                    $this->_data['title'] 		  = 'Jewel Shop | '.ucfirst($head_name['menu_name']);

                    $this->_data['content'] 	  = $this->view('product/listproduct', $data, TRUE);

                }
            }

            $this->view('layout', $this->_data);
        }//End function index()


        public function detail()
        {
            $slug = $_GET['slug'];

            /*CHECK EXISTS SLUG*/
            if($slug == '' || is_numeric($slug) || !isset($slug))
            {
                $this->_data['head_name'] = '404';
                $this->_data['title']     = 'Jewel Shop';
                $this->_data['content']   = $this->view('errors/errors', $data = '', TRUE);
            }
            else
            {
                /*GET PRODUCT*/
                $data['product'] = $this->_model->product->getProductBySlug(array('slug' => $slug));
                /*echo "<pre>"; print_r($data['product']);*/
                /*CHECK EXISTS PRODUCT*/
                if(!$data['product'])
                {
                    $this->_data['head_name'] = '404';
                    $this->_data['title']     = 'Jewel Shop';
                    $this->_data['content']   = $this->view('errors/errors', $data = '', TRUE);
                }
                else
                {
                    /*GET MENU NAME*/
                    $menu 				= $this->_model->menu->getMenuName(array('slug' => $data['product']['menu_slug']));

                    if($menu):
                        $data['cate']	= $menu['menu_name'];
                    else:
                        $data['cate'] 	= 'Sản phẩm';
                    endif;

                    /*GET RANDOM PRODUCTS*/
                    $data['random_products']  = $this->_model->product->getRandomProduct($data['product']['product_id']);

                    /*UPDATE COUNTER*/
                    $this->_model->product->updateCounter($data['product']['product_id']);

                    $this->_data['head_name'] = $menu['menu_name'];

                    $this->_data['title'] 	  = "Jewel Shop | ".ucfirst($data['product']['product_name']);

                    $this->_data['content']   = $this->view("product/detailproduct", $data, TRUE);
                }

            }

            $this->view('layout', $this->_data);

        }//End function detail()

	}


    /*End of product_controller.php*/
    /*Location: my_framework/controllers/product_controller.php */